//
//  AddressBookVC.m
//  Connections
//
//  Created by beisheng03 on 2017/10/11.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "AddressBookVC.h"
#import "MyAddressBookCell.h"
#import "AddresBookResultDisVC.h"

#import <LJContactManager/LJContactManager.h>
#import <LJContactManager/LJPerson.h>

@interface AddressBookVC ()<UISearchBarDelegate>
/**  */
@property (nonatomic, strong) NSArray<LJSectionPerson *> *dataSetionModels;

/** <#note#> */
@property (nonatomic, strong) NSArray<NSString *> *setionKeys;


@property (nonatomic, strong) UISearchController *searchController;


@end

@implementation AddressBookVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"通讯录";
    
    self.tableView.backgroundColor = colorWith(240, 240, 240);
    
    //右检索背景颜色
    self.tableView.sectionIndexBackgroundColor= [UIColor clearColor];
    
    //右检索字体颜色
    self.tableView.sectionIndexColor= themeColor;
    
    AddresBookResultDisVC *result = [[AddresBookResultDisVC alloc]initWithStyle:UITableViewStylePlain];
    
    // 创建搜索框
    UISearchController *search = [[UISearchController alloc]initWithSearchResultsController:result];
    
    self.searchController = search;
    
    search.searchResultsUpdater = result;

    //是否添加半透明覆盖层
    search.dimsBackgroundDuringPresentation = YES;
    //    //是否隐藏导航栏
    search.hidesNavigationBarDuringPresentation = YES;
    
    
    search.searchBar.placeholder = @"搜索";
    
    self.definesPresentationContext = YES;
    
//    search.searchBar.delegate = self;
    
    
    UIView *temp = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 98)];
    
    [temp addSubview:search.searchBar];
    
    temp.backgroundColor = [UIColor redColor];
    
    self.tableView.tableHeaderView = temp;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MyAddressBookCell class]) bundle:nil] forCellReuseIdentifier:[MyAddressBookCell identifierStr]];

    
    [[LJContactManager sharedInstance] accessSectionContactsComplection:^(BOOL succeed, NSArray<LJSectionPerson *> *contacts, NSArray<NSString *> *keys) {
        
        self.dataSetionModels = contacts;
        
        self.setionKeys = keys;
        
        [self.tableView reloadData];
        
        
    }];
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (self.dataSetionModels == nil) {
        return  0;
    }
    
    return self.dataSetionModels.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.dataSetionModels == nil) {
        return  0;
    }
    

    return self.dataSetionModels[section].persons.count;
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     
    MyAddressBookCell *cell = [tableView dequeueReusableCellWithIdentifier:[MyAddressBookCell identifierStr]];
     
     LJSectionPerson *section = self.dataSetionModels[indexPath.section];

     
     cell.textLabel.text = section.persons[indexPath.row].fullName;

 
 
    return cell;
 }


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 76;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 36;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    
    return [[UIView alloc]init];
}


- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    if (self.setionKeys == nil) {
        return  @[];
    }
    
    return self.setionKeys;
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
